//////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2022, Cinesite VFX Ltd. All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are
//  met:
//
//      * Redistributions of source code must retain the above
//        copyright notice, this list of conditions and the following
//        disclaimer.
//
//      * Redistributions in binary form must reproduce the above
//        copyright notice, this list of conditions and the following
//        disclaimer in the documentation and/or other materials provided with
//        the distribution.
//
//      * Neither the name of John Haddon nor the names of
//        any other contributors to this software may be used to endorse or
//        promote products derived from this software without specific prior
//        written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
//  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#include "GafferScene/VisibleSetData.h"

#include "GafferScene/Export.h"
#include "GafferScene/TypeIds.h"

#include "Gaffer/Context.h"

#include "IECore/PathMatcherData.h"
#include "IECore/TypedData.h"
#include "IECore/TypedData.inl"

namespace
{

Gaffer::Context::TypeDescription<GafferScene::VisibleSetData> g_visibleSetDataTypeDescription;
const IndexedIO::EntryID g_inclusionsEntry( "inclusions" );
const IndexedIO::EntryID g_exclusionsEntry( "exclusions" );
const IndexedIO::EntryID g_expansionsEntry( "expansions" );
const InternedString g_visibleSetEntry( "visibleSet" );
const unsigned int g_ioVersion = 0;

} // namespace

namespace IECore
{

IECORE_RUNTIMETYPED_DEFINETEMPLATESPECIALISATION( GafferScene::VisibleSetData, GafferScene::VisibleSetDataTypeId )

/// \todo Remove once Cortex has been updated to provide these.
template<>
void PathMatcherData::save( SaveContext *context ) const;

template<>
void PathMatcherData::load( LoadContextPtr context );

template<> GAFFERSCENE_API
void VisibleSetData::save( SaveContext *context ) const
{
	Data::save( context );
	IndexedIOPtr container = context->container( staticTypeName(), g_ioVersion );
	IndexedIOPtr visibleSetIO = container->subdirectory( g_visibleSetEntry, IndexedIO::CreateIfMissing );

	IECore::PathMatcherDataPtr inclusionsData = new IECore::PathMatcherData( readable().inclusions );
	IECore::PathMatcherDataPtr exclusionsData = new IECore::PathMatcherData( readable().exclusions );
	IECore::PathMatcherDataPtr expansionsData = new IECore::PathMatcherData( readable().expansions );

	context->save( inclusionsData.get(), visibleSetIO.get(), g_inclusionsEntry );
	context->save( exclusionsData.get(), visibleSetIO.get(), g_exclusionsEntry );
	context->save( expansionsData.get(), visibleSetIO.get(), g_expansionsEntry );
}

template<> GAFFERSCENE_API
void VisibleSetData::load( LoadContextPtr context )
{
	Data::load( context );
	unsigned int v = 0;
	ConstIndexedIOPtr container = context->container( staticTypeName(), v );
	ConstIndexedIOPtr visibleSetIO = container->subdirectory( g_visibleSetEntry, IndexedIO::NullIfMissing );

	writable().inclusions = context->load<PathMatcherData>( visibleSetIO.get(), g_inclusionsEntry )->readable();
	writable().exclusions = context->load<PathMatcherData>( visibleSetIO.get(), g_exclusionsEntry )->readable();
	writable().expansions = context->load<PathMatcherData>( visibleSetIO.get(), g_expansionsEntry )->readable();
}

template class TypedData<GafferScene::VisibleSet>;

} // namespace IECore
